package fr.mywiki.view.array.cell;

import fr.mywiki.model.ejbAdapter.Value;

/**
 * Displays the content of a specific property on a given length.
 * 
 * @author Thomas
 */
public class StringPropertyDisplayer implements CellType {
	/** If length takes this value, no limit is adopted. */
	private static final int NO_LENGTH_LIMIT = -1;

	/** Which length will the result have ? */
	private int length;

	/** The name of the property to display */
	private String propName;
	
	/** Is this element clickable ? */
	private boolean clickable;

	private StringPropertyDisplayer() {
	}

	public StringPropertyDisplayer(String propName, int length) {
		this(propName, length, false);
	}
	
	public StringPropertyDisplayer(String propName, int length, boolean clickable) {
		this.propName = propName;
		this.length = length;
		this.clickable = clickable;
	}

	public StringPropertyDisplayer(String propName) {
		this(propName, NO_LENGTH_LIMIT, false);
	}
	
	public StringPropertyDisplayer(String propName, boolean clickable) {
		this(propName, NO_LENGTH_LIMIT, clickable);
	}

	public String compute(Value value) {
		String complete = (String) value.get(propName);
		if (NO_LENGTH_LIMIT < length)
			complete = complete.substring(0, length);
		if(clickable)
			complete = "<a href=\"javascript:selectElement(" + value.getId() + ",'"
					+ value.getTypeEnt() + "');\">" + complete + "</a>";
		return complete;
	}
}